﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Drawing.Primitive
{
    public class Rectangle : BasePrimitive
    {

        public Rectangle()
            : base(Enum.EnumDrawingType.POLYGON, "Draw Rectangle", Enum.EnumMouseClicks.TWO)
        {
            properties = Property.DefaultProperty.Common;
            Initialize(0, 0, 0, 0);
        }

        public Rectangle(float x1, float y1, float x2, float y2)
            : base(Enum.EnumDrawingType.POLYGON, "Draw Rectangle", Enum.EnumMouseClicks.TWO)
        {
            properties = Property.DefaultProperty.Common;
            Initialize(x1, y1, x2, y2);
        }

        public Rectangle(Entity.Point2F point1, Entity.Point2F point2)
            : base(Enum.EnumDrawingType.POLYGON, "Draw Rectangle", Enum.EnumMouseClicks.TWO)
        {
            properties = Property.DefaultProperty.Common;
            Initialize(point1.X, point1.Y, point2.X, point2.Y);
        }

        public Rectangle(Entity.Point2I point1, Entity.Point2I point2)
            : base(Enum.EnumDrawingType.POLYGON, "Draw Rectangle", Enum.EnumMouseClicks.TWO)
        {
            properties = Property.DefaultProperty.Common;
            Initialize(point1.X, point1.Y, point2.X, point2.Y);
        }

        public Rectangle(Entity.Rectangle2F rectangle)
            : base(Enum.EnumDrawingType.POLYGON, "Draw Rectangle", Enum.EnumMouseClicks.TWO)
        {
            properties = Property.DefaultProperty.Common;
            Initialize(rectangle.Point1.X, rectangle.Point1.Y, rectangle.Point2.X, rectangle.Point2.Y);
        }

        private void Initialize(float x1, float y1, float x2, float y2)
        {
            if (x1 > x2)
            {
                float temp = x1;
                x1 = x2;
                x2 = temp;
            }

            if (y1 > y2)
            {
                float temp = y1;
                y1 = y2;
                y2 = temp;
            }

            points = new List<Entity.Point2F>();

            points.Add(new Entity.Point2F(x1, y1));
            points.Add(new Entity.Point2F(x1, y2));
            points.Add(new Entity.Point2F(x2, y2));
            points.Add(new Entity.Point2F(x2, y1));

            //Update the drawing after adding points.
            UpdateDrawing ();
            UpdateCenter();

        }

        public override GraphicBase Clone()
        {
            Rectangle graphic = new Rectangle();

            //Clone base properties
            base.Clone(graphic);

            return graphic;
        }

        public override void UpdateGuide(List<Entity.Point2F> mousedown, Entity.Point2F mousemove)
        {
            if (mousemove.X > mousedown[0].X)
                Initialize(mousedown[0].X, mousedown[0].Y, mousemove.X, mousemove.Y);
            else
                Initialize(mousemove.X, mousemove.Y, mousedown[0].X, mousedown[0].Y);
        }

    }
}
